package com.gomoku.controller;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;

/**
 * 游戏倒计时控制器
 * 功能：管理单局游戏总时长、步时限制、暂停/继续逻辑
 */
class TimerController {
    private Timeline timeline;
    private int totalSeconds = 0;
    private boolean isPaused = false;

    // 绑定到GameController的UI更新接口
    public interface TimerUpdateCallback {
        void onTimeUpdate(String formattedTime);
    }

    public void start(TimerUpdateCallback callback) {
        timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> {
            if (!isPaused) {
                totalSeconds++;
                callback.onTimeUpdate(formatTime(totalSeconds));
            }
        }));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    public void pause() {
        isPaused = true;
    }

    public void resume() {
        isPaused = false;
    }

    public void stop() {
        if (timeline != null) {
            timeline.stop();
        }
    }

    private String formatTime(int seconds) {
        return String.format("%02d:%02d:%02d",
                seconds / 3600, (seconds % 3600) / 60, seconds % 60);
    }

    public int getTotalSeconds() {
        return totalSeconds;
    }
}